//
//  PhotoPickView.swift
//  TicketSystem
//
//  Created by 木瓜 on 2017/2/9.
//  Copyright © 2017年 UK. All rights reserved.
//

import Foundation
import ReactiveSwift

import Result

extension T {
    
    // MARK: - 图片选择
    class Photo: NSObject , ZLPhotoPickerViewControllerDelegate{
        static let sharedInstance = Photo()
        var callBack: ((_ assets: [Any]) -> Void)!
        func pickerViewControllerDoneAsstes(_ assets: [Any]!) {
            self.callBack(assets)
        }
    }
    
    
    /// 图片选择器
    ///
    /// - Parameters:
    ///   - vc: Current ViewController
    ///   - supportMultipleSelect: 是否多选
    /// - Returns: [ZLCamera] 或者 [ZLPhotoAssets]
    static func pickerImageSignal(fromViewController vc: UIViewController, selectMaxCount: Int) -> SignalProducer<[ImageInfoProperty], NoError> {
        
        return SignalProducer<SignalProducer<[ImageInfoProperty], NoError>, NoError> {observer, disposable in
            
            let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
            let cancelAction = UIAlertAction(title: "取消", style: .cancel, handler: nil)
            let takePhotoAction = UIAlertAction.init(title: "拍照", style: .default, handler: {(action) in
                observer.send(value: self.pickMultipleImageFrom(viewController: vc, sourceType: .camera, selectMaxCount: selectMaxCount))
                observer.sendCompleted()
            })
            let pickPhotoAction = UIAlertAction(title: "从相册选择", style: .default, handler: {(action) in
                observer.send(value: self.pickMultipleImageFrom(viewController: vc, sourceType: .photoLibrary, selectMaxCount: selectMaxCount))
                observer.sendCompleted()
            })
            alertController.addAction(pickPhotoAction)
            alertController.addAction(takePhotoAction)
            alertController.addAction(cancelAction)
            vc.present(alertController, animated: true, completion: nil)
            
            }.flatMap(.latest, transform: { (signal) -> SignalProducer<[ImageInfoProperty], NoError> in
                return SignalProducer<[ImageInfoProperty], NoError> {observer, disposable in
                    signal.startWithResult({ (result) in
                        switch result {
                        case .success(let imageEntitys):
                            observer.send(value: imageEntitys)
                            observer.sendCompleted()
                        default :
                            break
                        }
                    })
                }
            })
    }
    
    // 获取照片拍照
    static private func pickMultipleImageFrom(viewController: UIViewController, sourceType: UIImagePickerControllerSourceType, selectMaxCount: Int) -> SignalProducer<[ImageInfoProperty], NoError>{
        
        return SignalProducer<[ImageInfoProperty], NoError> {observer, disposable in
            
            switch sourceType{
            case .camera:
                let camera = ZLCameraViewController()
                camera.maxCount = selectMaxCount
                camera.callback = { result in
                    let infos = result as! [ZLCamera]
                    observer.send(value: infos)
                    observer.sendCompleted()
                }
                camera .showPickerVc(viewController)
            case .photoLibrary:
                // [ZLPhotoAssets]
                let picker = ZLPhotoPickerViewController()
                picker.maxCount = selectMaxCount
                picker.delegate = Photo.sharedInstance
                Photo.sharedInstance.callBack = { result in
                    let infos = result as! [ZLPhotoAssets]
                    observer.send(value: infos)
                    observer.sendCompleted()
                }
                picker.showPickerVc(viewController)
            default:
                break
            }
            
        }
        
    }
    
    
}



@objc protocol ImageInfoProperty {
    func getpathOfImage() -> String
    func getThumnail() -> UIImage
    func getImage() -> UIImage
}

extension ZLCamera: ImageInfoProperty{
    func getpathOfImage() -> String {
        return self.imagePath
    }
    
    func getThumnail() -> UIImage {
        return self.thumbImage
    }
    
    func getImage() -> UIImage {
        return self.photoImage
    }
}

extension ZLPhotoAssets: ImageInfoProperty{
    func getpathOfImage() -> String {
        return self.asset.defaultRepresentation().url().absoluteString
    }
    
    func getThumnail() -> UIImage {
        return self.thumbImage()
    }
    
    func getImage() -> UIImage {
        return self.originImage()
    }
    
}

